﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using DriveManagement;
using WinAPI;
namespace GUI
{
    public partial class DriveInfoControl : UserControl
    {
        private DriveInfo info;

        private PassGet passForm;

        public delegate void MountWithPassEventHandler(string password);

        public DriveInfoControl(char letter, string fileName)
        {
            InitializeComponent();
            info = new DriveInfo(fileName, letter);
            passForm = new PassGet();
            passForm.MountEvent += Mount;
            mountBtn.Text = Resources.Strings.Mount;
            contextMenu.Items[1].Enabled = false;
            contextMenu.Items[0].Enabled = true;
            driveNameLabel.Text = letter + "(" + info.FileNameWithoutExtension + "):\\";
        }
        public DriveInfoControl(char letter, string fileName, string password, Int64 size)
        {
            InitializeComponent();
            passForm = new PassGet();
            info = DriveManagement.DriveManager.Mount(fileName, size, letter, password);
            BackgroundImage = Properties.Resources.Mounted;
            mountBtn.Text = Resources.Strings.Unmount;
            passForm.MountEvent += Mount;
            mountBtn.Text = Resources.Strings.Unmount;
            contextMenu.Items[0].Enabled = false;
            contextMenu.Items[1].Enabled = true;
            driveNameLabel.Text = letter + "(" + info.FileNameWithoutExtension + "):\\";
        }

        private void MountBtn_Click(object sender, System.EventArgs e)
        {
            if (info.Mounted)
            {
                try
                {
                    Unmount();
                }
                catch (MarshalException ex)
                {
                    if (ex.ErrorCode == 5)
                    {
                        MessageBox.Show(Resources.Strings.DriveIsBusy);
                    }
                }
            }
            else
            {
                if (passForm.IsDisposed)
                {
                    passForm = new PassGet();
                    passForm.MountEvent += Mount;
                }
                passForm.ShowDialog();//on OKbtn_Click event handler is mounting 
            }
        }

        private void Mount(string password)
        {
            try
            {
                if (info.Mounted == false)
                {
                    info = DriveManager.Mount(info.FileName, new System.IO.FileInfo(info.FileName).Length, info.Letter, password);
                    mountBtn.Text = Resources.Strings.Unmount;
                    contextMenu.Items[0].Enabled = false;
                    contextMenu.Items[1].Enabled = true;
                    BackgroundImage = Properties.Resources.Mounted;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Resources.Strings.MountFailed + ex.Message, Resources.Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Unmount()
        {
            try
            {
                if (info.Mounted == true)
                {
                    DriveManager.Unmount(info);
                    mountBtn.Text = Resources.Strings.Mount;
                    contextMenu.Items[1].Enabled = false;
                    contextMenu.Items[0].Enabled = true;
                    BackgroundImage = Properties.Resources.Dismounted;
                }
            }
            catch (Exception ex)
            {
                if (ex is MarshalException)
                    throw;//drive is busy, send exception further
                else
                    MessageBox.Show(Resources.Strings.UnmountingFailed + ex.Message, Resources.Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (info.Mounted)
                    Unmount();
                Parent.Controls.Remove(this);
            }
            catch (MarshalException ex)
            {//"съели" исключение, если это "drive is busy", иначе - бросаем дальше
                if (ex.ErrorCode == 5)
                    MessageBox.Show(Resources.Strings.DriveIsBusy);
                else throw;
            }
        }

        private void DriveInfoControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenu.Show(Cursor.Position);
            }
        }

        public DriveInfo Info
        {
            get { return info; }
        }

        private void DriveInfoControl_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Process.Start("explorer.exe", info.Letter + ":\\");
            }
            catch (Exception) { }
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(info.Mounted)
                FilePropertiesManager.ShowFilePropertyDlg(info.Letter + ":\\");
            else
                FilePropertiesManager.ShowFilePropertyDlg(info.FileName);
        }
    }
}
